package com.itheima.travel.service.impl;

import com.itheima.travel.config.SnowflakeIdWorker;
import com.itheima.travel.constant.RedisConstant;
import com.itheima.travel.mapper.AffixMapper;
import com.itheima.travel.pojo.Affix;
import com.itheima.travel.pojo.AffixExample;
import com.itheima.travel.req.AffixVo;
import com.itheima.travel.service.AffixService;
import com.itheima.travel.utils.BeanConv;
import com.itheima.travel.utils.EmptyUtil;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service("affixService")
public class AffixServiceImpl implements AffixService {

    @Autowired
    AffixMapper affixMapper;

    @Autowired
    SnowflakeIdWorker snowflakeIdWorker;

    @Value("${upLoad.pathRoot}")
    String pathRoot;

    @Autowired
    RedissonClient redissonClient;

    @Override
    public AffixVo upload(MultipartFile multipartFile, AffixVo affixVo) throws IOException {
        //判断上传的文件是否为空
        if(multipartFile==null){
            return null;
        }
        String businessType = affixVo.getBusinessType();
        //关联业务
        affixVo.setBusinessType(businessType);
        // 原始上传的文件名称aaa.jpg
        String originalFilename = multipartFile.getOriginalFilename();
        //后缀名称.jpg
        String suffix = originalFilename
                .substring(originalFilename.lastIndexOf("."));
        affixVo.setSuffix(suffix);
        //文件名称
        String fileName = String.valueOf(snowflakeIdWorker.nextId());
        affixVo.setFileName(fileName);
        //构建访问路径
        String pathUrl=businessType+"/"+fileName+suffix;
        //判断业务类型的文件夹是否存在;文件夹不存在则创建
        File file=new File(pathRoot+businessType);
        if (!file.exists()){
            file.mkdir();
        }
        file=new File(pathRoot+"/"+pathUrl);

        multipartFile.transferTo(file);
        affixVo.setPathUrl(pathUrl);
        Affix affix = BeanConv.toBean(affixVo, Affix.class);
        affixMapper.insert(affix);
        //细节主键自增回填
        return BeanConv.toBean(affix, AffixVo.class);
    }

    @Override
    public Boolean bindBusinessId(AffixVo affixVo) {
        Affix affix = BeanConv.toBean(affixVo, Affix.class);
        int flag = affixMapper.updateByPrimaryKeySelective(affix);
        return flag>0;
    }

    @Override
    public List<AffixVo> findAffixByBusinessId(AffixVo affixVo) {
        //读取缓存
        String key= RedisConstant.AFFIXSERVICE_FINDAFFIXBYBUSINESSID+affixVo.getBusinessId();
        RBucket<List<AffixVo>> bucket = redissonClient.getBucket(key);
        List<AffixVo> affixVosCache = bucket.get();
        if (!EmptyUtil.isNullOrEmpty(affixVosCache)){
            return affixVosCache;
        }

        //缓存未读取到数据时从数据库读取
        AffixExample example = new AffixExample();
        example.createCriteria()
                .andBusinessIdEqualTo(affixVo.getBusinessId());
        List<Affix> affixes = affixMapper.selectByExample(example);
        //更新缓存
        if (!EmptyUtil.isNullOrEmpty(affixes)){
            bucket.set(BeanConv.toBeanList(affixes,AffixVo.class),1800, TimeUnit.SECONDS);
        }
        return BeanConv.toBeanList(affixes,AffixVo.class);
    }
}
